﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Warehouse.DataModel;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.ComponentModel;
using System.Collections.Specialized;
using Warehouse;
using System.Collections;
using System.Data.Entity;
using System.Printing;
using System.Windows.Xps;
using System.Windows.Controls.Primitives;
using SoftekBarcodeMakerLib2;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace Test
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public BitmapSource ToWpfBitmap(Bitmap bitmap)
        {
            var bi = new BitmapImage();
            bi.BeginInit();
            var ms = new MemoryStream();
            bitmap.Save(ms, ImageFormat.Bmp);
            ms.Seek(0, SeekOrigin.Begin);
            bi.StreamSource = ms;
            bi.EndInit();
            return bi;
        }

        BitmapSource GetBarCodeImage(string upc)
        {
            using (var maker = new BarcodeMaker { barcode = upc, Type = 2, ModuleSize = 2 })
            {
                var bi = new BitmapImage();
                bi.BeginInit();
                var ms = new MemoryStream();
                maker.MakeBarcode().Save(ms, ImageFormat.Bmp);
                ms.Seek(0, SeekOrigin.Begin);
                bi.StreamSource = ms;
                bi.EndInit();
                return bi;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            var data =
                from p in new Repository().Brands.Where(b => b.Name == "Thermafuse").Single().Products().Take(8).AsEnumerable()
                select new Dictionary<string, object>
                {
                    { "BarCodeImage", GetBarCodeImage(p.SKU) },
                    { "Brand", p.ProductName.Brand.Name },
                    { "Product", p.ProductName.Name },
                    { "Size", String.Format("{0} {1}", p.SizeOrColor, p.Unit.Name).Trim() },
                };

            this.BarCodeItemControl.DataContext = data;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            PrintDocumentImageableArea area = null;
            XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(ref area);
            if (xpsdw != null)
            {
                double leftMargin = 24;    
                double topMargin = 24;    
                double rightMargin = 24;   
                double bottomMargin = 24;
                var outputSize = new System.Windows.Size(area.MediaSizeWidth, area.MediaSizeHeight);

                Debug.Assert(leftMargin >= area.OriginWidth);
                Debug.Assert(topMargin >= area.OriginHeight);
                Debug.Assert(rightMargin <= area.OriginWidth + area.ExtentWidth);
                Debug.Assert(topMargin <= area.OriginHeight + area.ExtentHeight);

                var container = new BarCodeItemControl();
                container.DataContext = 
                    from p in new Repository().Brands.Where(b => b.Name == "Thermafuse").Single().Products().Take(7).AsEnumerable()
                    select new Dictionary<string, object>
                    {
                        { "BarCodeImage", GetBarCodeImage(p.SKU) },
                        { "Brand", p.ProductName.Brand.Name },
                        { "Product", p.ProductName.Name },
                        { "Size", String.Format("{0} {1}", p.SizeOrColor, p.Unit.Name).Trim() },
                    };

                container.Margin = new Thickness(leftMargin, topMargin, rightMargin, bottomMargin);
                container.Measure(outputSize);
                container.Arrange(new Rect(outputSize));
                container.UpdateLayout();
                xpsdw.Write(container);
            }
        }
    }
}
